package com.example.dbcp;

import org.apache.commons.dbcp.BasicDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;

/**
 * @auther 尚智江
 * @Date 2022/5/14 23:31
 */
public class Example01 {
    // 手动配置 数据库连接信息
    public static DataSource dateSource = null;
    static {
        // 获取 DBCP 数据库连接池实现类对象
        BasicDataSource basicDataSource = new BasicDataSource();
        // 设置连接数据库需要的配置信息
        basicDataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        basicDataSource.setUrl("jdbc:mysql://localhost:3306/jdbc?serverTimezone=GMT%2B8");
        basicDataSource.setUsername("root");
        basicDataSource.setPassword("1234");
        // 设置数据库连接池的初始化参数
        basicDataSource.setInitialSize(5);
        dateSource = basicDataSource;
    }

    public static void main(String[] args) throws SQLException {
        // 获取数据库连接对象
        Connection connection = dateSource.getConnection();
        // 获取数据库连接信息
        DatabaseMetaData databaseMetaData = connection.getMetaData();
        // 打印数据库信息
        System.out.println("数据库 URL 信息：" + databaseMetaData.getURL() + "\n"
                +"username：" + databaseMetaData.getUserName() + "\n"
                +"数据库驱动：" + databaseMetaData.getDriverName());
    }
}
